home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / nt / utilnt.c < prev   
Encoding:
C/C++ Source or Header  |  1996-05-17  |  8.8 KB  |  340 lines

  1. /* Various utilities - NT versions
  2.    Copyright (C) 1994, 1995, 1996 the Free Software Foundation.
  3.  
  4.    Written 1994, 1995, 1996 by:
  5.    Juan Grigera, Miguel de Icaza, Janne Kukonlehto, Dugan Porter,
  6.    Jakub Jelinek, Mauricio Plaza.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License, or
  11.    (at your option) any later version.
  12.    
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <config.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <sys/types.h>
  26. #include <config.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <sys/types.h>
  30. #include <windows.h>
  31. #include <io.h>
  32. #include <fcntl.h>
  33. #include <signal.h>        /* my_system */
  34. #include <limits.h>        /* INT_MAX */
  35. #include <sys/time.h>        /* select: timeout */
  36. #include <sys/param.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <stdarg.h>
  40. #include <process.h>
  41. #include <fs.h>
  42. #include <util.h>
  43.  
  44. char *get_owner (int uid)
  45. {
  46.     return "none";
  47. }
  48.  
  49. char *get_group (int gid)
  50. {
  51.     return "none";
  52. }
  53.  
  54. /* Pipes are guaranteed to be able to hold at least 4096 bytes */
  55. /* More than that would be unportable */
  56. #define MAX_PIPE_SIZE 4096
  57.  
  58. static int error_pipe[2];    /* File descriptors of error pipe */
  59. static int old_error;        /* File descriptor of old standard error */
  60.  
  61. /* Creates a pipe to hold standard error for a later analysis. */
  62. /* The pipe can hold 4096 bytes. Make sure no more is written */
  63. /* or a deadlock might occur. */
  64. void open_error_pipe (void)
  65. {
  66.     if (pipe (error_pipe) < 0){
  67.     message (0, " Warning ", " Pipe failed ");
  68.     }
  69.     old_error = dup (2);
  70.     if(old_error < 0 || close(2) || dup (error_pipe[1]) != 2){
  71.     message (0, " Warning ", " Dup failed ");
  72.     close (error_pipe[0]);
  73.     close (error_pipe[1]);
  74.     }
  75.     close (error_pipe[1]);
  76. }
  77.  
  78. void close_error_pipe (int error, char *text)
  79. {
  80.     char *title;
  81.     char msg[MAX_PIPE_SIZE];
  82.     int len = 0;
  83.  
  84.     if (error)
  85.     title = " Error ";
  86.     else
  87.     title = " Warning ";
  88.     if (old_error >= 0){
  89.     close (2);
  90.     dup (old_error);
  91.     close (old_error);
  92.     len = read (error_pipe[0], msg, MAX_PIPE_SIZE);
  93.  
  94.     if (len >= 0)
  95.         msg[len] = 0;
  96.     close (error_pipe[0]);
  97.     }
  98.     if (error < 0)
  99.     return;        /* Just ignore error message */
  100.     if (text == NULL){
  101.     if (len == 0) return;    /* Nothing to show */
  102.  
  103.     /* Show message from pipe */
  104.     message (error, title, msg);
  105.     } else {
  106.     /* Show given text and possible message from pipe */
  107.     message (error, title, " %s \n %s ", text, msg);
  108.     }
  109. }
  110.  
  111. void check_error_pipe (void)
  112. {
  113.     char error[MAX_PIPE_SIZE];
  114.     int len = 0;
  115.     if (old_error >= 0){
  116.     while (len < MAX_PIPE_SIZE)
  117.     {
  118.         int rvalue;
  119.  
  120.         rvalue = read (error_pipe[0], error + len, 1);
  121.         len ++;
  122.         if (rvalue <= 0)
  123.         break;
  124.     }
  125.     error[len] = 0;
  126.     close (error_pipe[0]);
  127.     }
  128.     if (len > 0)
  129.         message (0, " Warning ", error);
  130. }
  131.  
  132. int my_system (int as_shell_command, const char *shell, const char *command)
  133. {
  134.     int status = 0;
  135.  
  136.     if (as_shell_command)
  137.        spawnlp (_P_WAIT, shell, shell, "/c", command, (char *) 0);
  138.     else
  139.        spawnl (_P_WAIT, shell, shell, command, (char *) 0);
  140.  
  141.     return status;
  142. }
  143.  
  144. char *tilde_expand (char *directory)
  145. {
  146.     return strdup (directory);
  147. }
  148.  
  149. /* Canonicalize path, and return a new path. Do everything in situ.
  150.  * [call OS API]
  151.  */
  152. char *canonicalize_pathname (char *path)
  153. {
  154. /* This holds an unused pointer to the start of file name in path */
  155.     char *pName; 
  156.     char pCanonical[MC_MAXPATHLEN];
  157.  
  158.     GetFullPathName (path, MC_MAXPATHLEN, pCanonical, &pName);
  159.  
  160.     /* FIXME: buffer large enough? */
  161.     strcpy (path, pCanonical);
  162. }
  163.  
  164.  
  165. void my_statfs (struct my_statfs *myfs_stats, char *path)
  166. {
  167.     int i, len = 0;
  168.     DWORD lpSectorsPerCluster, lpBytesPerSector, lpFreeClusters, lpClusters;
  169.        DWORD           lpMaximumComponentLength, dw, lpFileSystemFlags;
  170.        static char     lpVolumeNameBuffer[256], lpFileSystemNameBuffer[30];
  171.  
  172.        GetDiskFreeSpace(NULL, &lpSectorsPerCluster, &lpBytesPerSector,
  173.             &lpFreeClusters, &lpClusters);
  174.  
  175.        /* KBytes available */
  176.        myfs_stats->avail = lpSectorsPerCluster * lpBytesPerSector * lpFreeClusters / 1024;
  177.        
  178.        /* KBytes total */
  179.        myfs_stats->total = lpSectorsPerCluster * lpBytesPerSector * lpClusters / 1024; 
  180.        myfs_stats->nfree = lpFreeClusters;
  181.        myfs_stats->nodes = lpClusters;
  182.  
  183.        GetVolumeInformation(NULL, lpVolumeNameBuffer, 255, NULL,
  184.                 &lpMaximumComponentLength, &lpFileSystemFlags,
  185.                 lpFileSystemNameBuffer, 30);
  186.  
  187.        myfs_stats->mpoint = lpFileSystemNameBuffer;
  188.        myfs_stats->device = lpVolumeNameBuffer;
  189.  
  190.  
  191.        myfs_stats->type = GetDriveType(NULL);
  192.        switch (myfs_stats->type) {
  193.        /*
  194.         * mmm. DeviceIoControl may fail if you are not root case
  195.         * F5_1Pt2_512,            5.25", 1.2MB,  512 bytes/sector
  196.         * myfs_stats->typename = "5.25\" 1.2MB"; break; case
  197.         * F3_1Pt44_512,           3.5",  1.44MB, 512 bytes/sector
  198.         * myfs_stats->typename = "3.5\" 1.44MB"; break; case
  199.         * F3_2Pt88_512,           3.5",  2.88MB, 512 bytes/sector
  200.         * myfs_stats->typename = "3.5\" 2.88MB"; break; case
  201.         * F3_20Pt8_512,           3.5",  20.8MB, 512 bytes/sector
  202.         * myfs_stats->typename = "3.5\" 20.8MB"; break; case
  203.         * F3_720_512,             3.5",  720KB,  512 bytes/sector
  204.         * myfs_stats->typename = "3.5\" 720MB"; break; case
  205.         * F5_360_512,             5.25", 360KB,  512 bytes/sector
  206.         * myfs_stats->typename = "5.25\" 360KB"; break; case
  207.         * F5_320_512,             5.25", 320KB,  512 bytes/sector
  208.         * case F5_320_1024,       5.25", 320KB,  1024
  209.         * bytes/sector myfs_stats->typename = "5.25\" 320KB"; break;
  210.         * case F5_180_512,        5.25", 180KB,  512
  211.         * bytes/sector myfs_stats->typename = "5.25\" 180KB"; break;
  212.         * case F5_160_512,        5.25", 160KB,  512
  213.         * bytes/sector myfs_stats->typename = "5.25\" 160KB"; break;
  214.         * case RemovableMedia,    Removable media other than
  215.         * floppy myfs_stats->typename = "Removable"; break; case
  216.         * FixedMedia              Fixed hard disk media
  217.         * myfs_stats->typename = "Hard Disk"; break; case Unknown:
  218.         * Format is unknown
  219.         */
  220.        case DRIVE_REMOVABLE:
  221.                myfs_stats->typename = "Removable";
  222.                break;
  223.        case DRIVE_FIXED:
  224.                myfs_stats->typename = "Hard Disk";
  225.                break;
  226.        case DRIVE_REMOTE:
  227.                myfs_stats->typename = "Networked";
  228.                break;
  229.        case DRIVE_CDROM:
  230.                myfs_stats->typename = "CD-ROM";
  231.                break;
  232.        case DRIVE_RAMDISK:
  233.                myfs_stats->typename = "RAM disk";
  234.                break;
  235.        default:
  236.                myfs_stats->typename = "unknown";
  237.                break;
  238.        };
  239. }
  240.  
  241. int gettimeofday (struct timeval* tvp, void *p)
  242. {
  243.     if (p != NULL)        // what is "p"?
  244.         return 0;    
  245.     
  246.     // Since MC only calls this func from get_random_hint we return 
  247.     // some value, not exactly the "correct" one
  248.     tvp->tv_sec = GetTickCount()/1000;     // Number of milliseconds since Windows started
  249.     tvp->tv_usec = GetTickCount();
  250. }
  251.  
  252. // FAKE funcs
  253.  
  254. /* lstat - Because of symlinks in Unix, stat will give info 
  255.        on the file pointed to and lstat on the symlink itself.
  256.        We have no such a difference/trouble.
  257.  */
  258. int lstat (const char* pathname, struct _stat *buffer)
  259. {
  260.     return stat (pathname, buffer);
  261. }
  262.  
  263. int geteuid ()    
  264. {
  265.     char User[15];
  266.     DWORD dw = 15;
  267.  
  268.     if (GetUserName(User, &dw) &&
  269.     !strcmp (User, "Administrator"))
  270.     return 0;
  271.     return 1;
  272. }
  273.  
  274. int getuid ()          
  275. {
  276. /*    SID sid;
  277.     LookupAccountName (NULL, &sid...
  278.     return 0;
  279. */
  280.     return 0;
  281. }
  282.  
  283. int getgid ()          
  284. {
  285.     return 0;
  286. }
  287.  
  288. int readlink (char* path, char* buf, int size)
  289. {
  290.     return -1;
  291. }
  292. int symlink (char *n1, char *n2)
  293. {
  294.     return -1;
  295. }
  296. int link (char *p1, char *p2)
  297. {
  298.     return -1;
  299. }
  300. int chown (char *path, int owner, int group)
  301. {
  302.     return -1;
  303. }
  304. int mknod (char *path, int mode, int dev)
  305. {
  306.     return -1;
  307. }
  308.  
  309. void init_uid_gid_cache (void)
  310. {
  311.     return 0;
  312. }
  313.  
  314. /*
  315. #define __SECURITY_DEFAULT   64
  316. int _stat (const char *pathname, struct _stat *buffer)
  317. {
  318.     SECURITY_DESCRIPTOR  sd;
  319.     BOOL                 flag;
  320.     DWORD                 dw;
  321.     SID                     sid;
  322.  
  323.      stat (pathname, buffer)
  324.  
  325.     sd = malloc (__SECURITY_DEFAULT);
  326.     GetFileSecurity (pathname, OWNER_SECURITY_INFORMATION, &sd, __SECURITY_DEFAULT, &dw);
  327.  
  328.     // Buffer too small, try again    
  329.     if (dw) {
  330.         free (sd);
  331.         sd = malloc (dw);
  332.         GetFileSecurity (pathname, OWNER_SECURITY_INFORMATION, &sd, __SECURITY_DEFAULT, &dw);
  333.     }
  334.  
  335.     GetSecurityDescriptorGroup (&sd, &sid, &flag);
  336.     unsigned short st_mode;
  337.     short st_uid;
  338.     short st_gid;
  339. }
  340.